package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.pojo.Employee;
import com.ssm.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Author  zxy
 * Date  2022/11/24 下午 4:07
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/employee/page/{pageNum}", method = RequestMethod.GET)
    public String getEmployeePage(@PathVariable("pageNum") Integer pageNum, Model model){
        // 获取员工的分页信息
        PageInfo<Employee> page = employeeService.getAllEmployeePage(pageNum);
        // 将分页数据共享到请求域
        model.addAttribute("page", page);
        // 跳转页面
        return "employee_list";
    }

    @RequestMapping(value = "/employee", method = RequestMethod.GET)
    public String getAllEmployee(Model model){
        // 查询所有员工信息
        List<Employee> list = employeeService.getAllEmployee();
        // 将员工信息共享到请求域
        model.addAttribute("list", list);
        // 跳转到employee_list.html
        return "employee_list";
    }

    /*
    * 根据id查询员工
    * */
    @RequestMapping(value = "/employee/{empId}", method = RequestMethod.GET)
    public String getEmployeeById(@PathVariable("empId") Integer empId, Model model) {
        Employee employee = employeeService.getEmployeeById(empId);
        model.addAttribute("employee", employee);
        return "employee_info";
    }

    /*
    * 修改员工信息
    * */
    @RequestMapping(value = "/employee", method = RequestMethod.PUT)
    public String updateEmployee(Employee employee) {
        employeeService.updateEmployee(employee);
        return "redirect:/employee/page/1";
    }

    /*
    * 删除员工信息
    * */
    @RequestMapping(value = "/employee/{empId}", method = RequestMethod.DELETE)
    public String deleteEmployeeById(@PathVariable("empId") Integer empId) {
        employeeService.deleteEmployeeById(empId);
        return "redirect:/employee/page/1";
    }

    /*
    * 新增员工
    * */
    @RequestMapping(value = "/employee", method = RequestMethod.POST)
    public String addEmployee(Employee employee) {
        employeeService.addEmployee(employee);
        return "redirect:/employee/page/1";
    }

    /*
    * 新增员工
    * */
    @RequestMapping(value = "/to/add", method = RequestMethod.GET)
    public String addEmployee(){
        return "employee_add";
    }
}
